package com.bzqn.baseframe;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageButton;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BaseNavicationBar extends RelativeLayout implements ViewUnitContent{

	private TextView mTitleTextView;
	private ImageButton mLeftButton;
	private ImageButton mRightButton;
	private NavigationBarConfig mHeaderConfig;
	public void setLeftButtonOnClickListener(OnClickListener listener){
		if(getLeftButton() != null){
			getLeftButton().setOnClickListener(listener);
		}
	}
	public void setRightButtonOnClickListener(OnClickListener listener){
		if(getRightButton() != null){
			getRightButton().setOnClickListener(listener);
		}
	}
	public BaseNavicationBar(Context context , AttributeSet attr) {
		super(context,attr);
		init(context);
	}
	public BaseNavicationBar(Context context){
		super(context);
		init(context);
	}
	public BaseNavicationBar(Context context,NavigationBarConfig config) {
		super(context);
		this.mHeaderConfig = config;
		init(context);
	}
	private void init(Context context) {
		if(mHeaderConfig == null){
			//create default config instance
			mHeaderConfig = new NavigationBarConfig();
		}
		if(mHeaderConfig.getNavigationBackgroundResId() != 0){
			setBackgroundResource(mHeaderConfig.getNavigationBackgroundResId());
		} else if(mHeaderConfig.getNavigationBackgroundColor().startsWith("http://") || mHeaderConfig.getNavigationBackgroundColor().startsWith("HTTP://")){
			setBackgroundColor(Color.GRAY);
			//如果是在线图片
			FinalBitmap.create(context).display(this, mHeaderConfig.getNavigationBackgroundColor());
		} else {
			setBackgroundColor(Color.parseColor(mHeaderConfig.getNavigationBackground()));
		}
		//add title
		RelativeLayout.LayoutParams baseHeaderTitleLayoutParams = new RelativeLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
		baseHeaderTitleLayoutParams.height = Utils.dip2px(mHeaderConfig.getNavigationHeight(),context);
		baseHeaderTitleLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
		mTitleTextView = new TextView(context);
		getTitleView().getPaint().setFakeBoldText(true);
		getTitleView().setTextColor(Color.parseColor(mHeaderConfig.getNavigationTitleTextColor()));
		getTitleView().setGravity(Gravity.CENTER);
		getTitleView().setTextSize(TypedValue.COMPLEX_UNIT_SP,mHeaderConfig.getNavigationTitleTextSize());
		getTitleView().setText(mHeaderConfig.getNavigationTitleText());
		addView(getTitleView(), baseHeaderTitleLayoutParams);
		//add left button
		setLeftButton(new ImageButton(context));
		
		RelativeLayout.LayoutParams baseHeaderLeftButtonLayoutParams = new RelativeLayout.LayoutParams(Utils.dip2px(mHeaderConfig.getLeftButtonWith(),context), Utils.dip2px(mHeaderConfig.getLeftButtonHeight(),context));
//		getLeftButton().setTextColor(Color.parseColor(mHeaderConfig.getLeftButtonTextColor()));
//		getLeftButton().setText(mHeaderConfig.getLeftButtonText());
		int lbPl = mHeaderConfig.getLeftButtonLeftPadding() == 0 ? getLeftButton().getPaddingLeft() : mHeaderConfig.getLeftButtonLeftPadding();
		int lbPt = mHeaderConfig.getLeftButtonTopPadding() == 0 ? getLeftButton().getPaddingTop() : mHeaderConfig.getLeftButtonTopPadding();
		int lbPr = mHeaderConfig.getLeftButtonRightPadding() == 0 ? getLeftButton().getPaddingRight() :mHeaderConfig.getLeftButtonRightPadding();
		int lbPd = mHeaderConfig.getLeftButtonBottomPadding() == 0? getLeftButton().getPaddingBottom() :mHeaderConfig.getLeftButtonBottomPadding();
		getLeftButton().setPadding(lbPl, lbPt, lbPr, lbPd);
//		getLeftButton().setBackgroundColor(Color.BLACK);
		if(mHeaderConfig.getLeftButtonBackgroundResId() != 0){
//			getLeftButton().setText("");
			mLeftButton.setImageBitmap(BitmapFactory.decodeResource(getResources(),mHeaderConfig.getLeftButtonBackgroundResId()));
		} else if(mHeaderConfig.getLeftButtonBackgroundColor().startsWith("http://") || mHeaderConfig.getLeftButtonBackgroundColor().startsWith("HTTP://")){
			//如果是在线图片
			getLeftButton().setBackgroundColor(Color.GRAY);
			FinalBitmap.create(context).display(getLeftButton(), mHeaderConfig.getLeftButtonBackgroundColor());
		} else {
			getLeftButton().setBackgroundColor(Color.parseColor(mHeaderConfig.getLeftButtonBackgroundColor()));
		}
//		getLeftButton().setTextSize(TypedValue.COMPLEX_UNIT_SP, mHeaderConfig.getLeftButtonTextSize());
		addView(getLeftButton(), baseHeaderLeftButtonLayoutParams);
		//add right button
		setRightButton(new ImageButton(context));
		RelativeLayout.LayoutParams baseHeaderRightButtonLayoutParams = new RelativeLayout.LayoutParams(mHeaderConfig.getRightButtonWith(), Utils.dip2px(mHeaderConfig.getRightButtonHeight(),context));
		baseHeaderRightButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//		getRightButton().setTextColor(Color.parseColor(mHeaderConfig.getRightButtonTextColor()));
//		getRightButton().setText(mHeaderConfig.getRightButtonText());
		int rbPl = mHeaderConfig.getRightButtonLeftPadding() == 0 ? getRightButton().getPaddingLeft() : mHeaderConfig.getRightButtonLeftPadding();
		int rbPt = mHeaderConfig.getRightButtonTopPadding() == 0 ? getRightButton().getPaddingTop() : mHeaderConfig.getRightButtonTopPadding();
		int rbPr = mHeaderConfig.getRightButtonRightPadding() == 0 ? getRightButton().getPaddingRight() :mHeaderConfig.getRightButtonRightPadding();
		int rbPd = mHeaderConfig.getRightButtonBottomPadding() == 0? getRightButton().getPaddingBottom() :mHeaderConfig.getRightButtonBottomPadding();
		getRightButton().setPadding(rbPl, rbPt, rbPr, rbPd);
		if(mHeaderConfig.getRightButtonBackgroundResId() != 0){
			getRightButton().setBackgroundResource(mHeaderConfig.getRightButtonBackgroundResId());
		} else if(mHeaderConfig.getRightButtonBackgroundColor().startsWith("http://") || mHeaderConfig.getRightButtonBackgroundColor().startsWith("HTTP://")){
			getRightButton().setBackgroundColor(Color.GRAY);
			FinalBitmap.create(context).display(getRightButton(), mHeaderConfig.getRightButtonBackgroundColor());
		} else {
			getRightButton().setBackgroundColor(Color.parseColor(mHeaderConfig.getRightButtonBackgroundColor()));
		}
		addView(getRightButton(), baseHeaderRightButtonLayoutParams);
		
	}
	public TextView getTitleView() {
		return mTitleTextView;
	}
	/**
	 * 添加到RelativeLayout容器上
	 * @param parent
	 */
	public void addToRelativeLayoutParent(RelativeLayout parent , Context context){
		RelativeLayout.LayoutParams baseHeaderLayoutParams = new RelativeLayout.LayoutParams(
				MATCH_PARENT, Utils.dip2px(mHeaderConfig.getNavigationHeight(),
						context));
		parent.addView(this, baseHeaderLayoutParams);
	}
	public ImageButton getLeftButton() {
		return mLeftButton;
	}
	public void setLeftButton(ImageButton mLeftButton) {
		mLeftButton.setScaleType(ScaleType.CENTER);
		mLeftButton.setBackgroundColor(Color.TRANSPARENT);
		this.mLeftButton = mLeftButton;
	}
	public ImageButton getRightButton() {
		return mRightButton;
	}
	public void setRightButton(ImageButton mRightButton) {
		mRightButton.setScaleType(ScaleType.CENTER);
		mRightButton.setBackgroundColor(Color.TRANSPARENT);
		this.mRightButton = mRightButton;
	}
}
